<?php
/**
 * Created by PhpStorm.
 * User: ma
 * Date: 2018/1/3
 * Time: 17:59
 */

namespace app\api\controller;


use app\common\model\Coupons as CouponsModel;
use app\common\model\Orders as OrdersModel;
use app\common\model\CashUsers as CashUsersModel;
use app\common\model\Record as RecordModel;
use app\common\model\Shops;
use app\common\model\User as UserModel;
use app\common\model\Shops as ShopsModel;
use app\common\model\Banks as BanksModel;
use app\common\model\UserAccount as UserAccountModel;
use think\Db;

define("USERSHOPLOGOURL",'https://cs.rongbaiwei.com/');
class UserCenter extends Base
{

    /**
     * 获取消费记录
     */
    public function consumeRecord(){
        $unionid=$this->request->param('unionid');//tok_key
        if(!$unionid){
            return $this->getOutput(4000,'unionid不能为空！');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->find();

        $model2=new OrdersModel();
        $where=[['ord_user_id','=',$user['id']],['ord_status','=',1]];
//        $return=$model2->where($where)->order('ord_create_time','desc')->select();
//        $return=$model2->field('ord_id,sp_name')->with('order_shops')->where($where)->order('ord_create_time','desc')->select()->toArray();
//        var_dump(123);die();
        $order=db('orders')
            ->alias('os')
            ->leftJoin('shops ss','os.ord_shop_id=ss.sp_id')
            ->where($where)
            ->field('sp_logo_url,sp_name,ord_create_time,ord_total_price')
            ->order('ord_create_time','desc')
            ->select();
         static $data2=array();
        foreach ($order as $key=>$value){

            foreach ($value as $k=>$v){

                if($k=='sp_logo_url'){
                    $data2[$key][$k]=USERSHOPLOGOURL.$v;
                }
                elseif($k=='ord_create_time'){
                    $data2[$key][$k]=date('Y-m-d H:i:s',$v);
                }elseif ($k=='ord_total_price'){
                    $s=$v/100;
                    $data2[$key][$k]="￥".$s."元";
                }
                else{
                    $data2[$key][$k]=$v;
                }

            }
//
        }



        if(!$order){
            return $this->getOUtput(404,'获取失败',$data2);

        }
        return $this->getOUtput(200,'ok',$data2);


    }
    /**
     * 获取提现记录
     */
    public function cashRecord(){
        $unionid=$this->request->param('unionid');
        if(!$unionid){
            return $this->getOutput(4000,'unionid不能为空');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->find();

//        $model2=new CashUserModl();
        $where=[['cu_user','=',$user['id']]];
        $order=db('cash_users')
            ->alias('cs')
            ->leftJoin('record rd','cs.cu_id=rd.re_cid')
            ->where($where)
            ->order('cu_time','desc')
            ->field('cu_order,cu_money,cu_time,cu_status,cu_type,re_state')
            ->select();
        static $data2=array();
        foreach ($order as $key=>$value){

            foreach ($value as $k=>$v){

               if($k=='cu_time'){
                    $data2[$key][$k]=date('Y-m-d H:i:s',$v);
                }elseif ($k=='cu_money'){
                    $s=$v/100;
                    $data2[$key][$k]="￥".$s."元";
                }elseif ($k=='cu_status'){
                    switch ($v){
                        case -1:
                             $data2[$key][$k]="待审核";
                             break;
                        case 1:
                             $data2[$key][$k]="审核成功";
                             break;
                        case 2:
                             $data2[$key][$k]="审核失败";
                             break;
                    }

               }elseif ($k=='cu_type'){
                   switch ($v){
                       case 1:
                           $data2[$key][$k]="微信提现";
                           break;
                       case 2:
                           $data2[$key][$k]="手动提现";
                           break;
                   }
               }elseif ($k=='re_state'){
                   switch ($v){
                       case -1:
                           $data2[$key][$k]="未打款";
                           break;
                       case 1:
                           $data2[$key][$k]="已打款";
                           break;
                   }
               }
                else{
                    $data2[$key][$k]=$v;
                }

            }
//
        }

        if(!$order){
            return $this->getOUtput(404,'获取失败',$data2);

        }
        return $this->getOUtput(200,'ok',$data2);


    }
    /**
     * 获取收入记录
     */
    public function incomeRecord(){
        $unionid=$this->request->param('unionid');
        if(!$unionid){
            return $this->getOutput(4000,'unionid 不能为空');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->find();

        $model2=new BanksModel();
        $where=[['bk_user','=',$user['id']],['bk_brokerage','>',0]];
        $return=$model2->where($where)->order('br_time','desc')->field('bk_brokerage,br_time')->select()->toArray();

        static $data2=array();
        foreach ($return as $key=>$value){

            foreach ($value as $k=>$v){

                if($k=='bk_brokerage'){
                    $s=$v/100;
                    $data2[$key][$k]="￥".$s."元";
                }
                else{
                    $data2[$key][$k]=$v;
                }

            }
//
        }
        if(!$return){
            return $this->getOUtput(404,'获取失败',$data2);

        }
        return $this->getOUtput(200,'ok',$data2);


    }
    /**
     * 完善资料
     */
    public function userDetail(){
        $unionid=$this->request->param('unionid');
        if(!$unionid){
            return $this->getOutput(4000,'unionid不能为空');
        }
        $user=\db('user')->where('unionid',$unionid)->field('gender,mobile,true_name,create_time')->find();

//        var_dump($user);die;
//        static $data2=array();
//
//
//            foreach ($user as $k=>$v){
//
//                if($k=='gender'){
//                    switch ($v){
//                        case 0:
//                            $data2[$k]='未知';
//                            break;
//                        case 1:
//                            $data2[$k]='男';
//                            break;
//                        case 2:
//                            $data2[$k]='女';
//                            break;
//
//                    }
//                }
//                else{
//                    $data2[$k]=$v;
//                }
//
//            }
//

        if(!$user){
            return $this->getOUtput(404,'获取失败',$user);

        }
        return $this->getOUtput(200,'ok',$user);


    }
    /**
     * 完善资料修改信息
     */
    public function updateUserDetail(){
        $request=$this->request;
        $unionid=$this->request->param('unionid');
        if(!$unionid){
            return $this->getOutput(4000,'unionid 不能为空');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->field('id')->find()->toArray();
        $updateData['gender']=$request->param('gender');
        $updateData['mobile']=$request->param('mobile');
        $updateData['true_name']=$request->param('true_name');

        if(empty($updateData['gender']||$updateData['mobile']||$updateData['true_name'])){
            return $this->getOUtput(401,'失败','不能全为空');

        }
        $res=$model->isUpdate(true)->save($updateData,['id'=>$user['id']]);

        if(!$res){
            return $this->getOUtput(404,'失败','');

        }
        return $this->getOUtput(200,'成功','');


    }
    /**
     * 红包
     */
    public function coupons(){
        $unionid = $this->request->param('unionid');
//        $key = 123;
        if($unionid){
            if(!$unionid){
                return $this->getOutput(4000,'unionid不能为空！');
            }
//            $openId = 1231321;
            // 用户ID
            $model = new UserModel();
            $user = db('user')->field('id')->where('unionid',$unionid)->find();
            // 用户红包余额 ua_coupons/100
            $account = db('user_account')->field('ua_coupons')->where('ua_user',$user['id'])->find();
            $data['account'] = sprintf('%.2f',$account['ua_coupons']/100);
            // 红包明细
            $coupons = db('coupons')
                    ->alias('c')
                    ->leftJoin('shops s','c.cp_shop_id=s.sp_id')
                    ->field('c.cp_price,c.cp_create_time,s.sp_name,s.sp_logo_url')
                    ->where('cp_user_id',$user['id'])
                    ->select();
            foreach($coupons as $k=>$v){
                $coupons[$k]['cp_price'] = sprintf('%.2f',$v['cp_price']/100);
                if($v['cp_create_time']) $coupons[$k]['cp_create_time'] = date('Y-m-d H:i:s',$v['cp_create_time']);
                if($v['sp_logo_url']) $coupons[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
            }
            $data['coupons'] = $coupons;
            return $this->getOutput('200','ok',$data);
        }else{
            return $this->getOutput(404,'获取失败');
        }
        
    }
    /**
     * 钱包
     */
    public function userPurse(){
        $unionid=$this->request->param('unionid');
        if(!$unionid){
            return $this->getOutput(400,'unionid 不能为空');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->find()->toArray();
        $where=[['ua_id','=',$user['id']]];
        $model2=new UserAccountModel();
        $return=$model2->where($where)->field('ua_account')->find()->toArray();
        $return['ua_account']=$return['ua_account']/100;
        $model3=new CashUsersModel();
        $return2=$model3->where('cu_user',$user['id'])->field('cu_id,cu_order,cu_wx_order,cu_money,cu_time,cu_status,cu_type')->select()->toArray();

        static $data2=array();
        foreach ($return2 as $key=>$value) {

            foreach ($value as $k => $v) {


                if ($k == 'cu_money') {
                    $s = $v / 100;
                    $data2[$key][$k] = "￥" . $s . "元";
                }elseif($k=='cu_status'){
                    switch ($v){
                        case -1:
                            $data2[$key][$k]="待审核";
                            break;
                        case 1:
                            $data2[$key][$k]="审核成功";
                            $model4=new RecordModel();
                            $res4=$model4->where('re_cid',$data2[$key]['cu_id'])->field('re_state,re_cashtime')->find()->toArray();
                                $data2[$key]['re_state']=$res4['re_state'];
                                $data2[$key]['re_cashtime']=$res4['re_cashtime'];

                            break;
                        case 2:
                            $data2[$key][$k]="审核失败";
                            break;
                    }

                }elseif($k=='cu_type'){
                    switch ($v){

                        case 1:
                            $data2[$key][$k]="微信提现";
                            break;
                        case 2:
                            $data2[$key][$k]="手动提现";
                            break;
                    }

                }
                else {
                    $data2[$key][$k] = $v;
                }

            }
        }

        static $data3=array();
        foreach ($data2 as $key=>$value){
            foreach ($value as $k=>$v){
                if($k=='cu_id'){

                }else{
                    $data3[$key][$k]=$v;

                }
            }
        }
//        $return3=array_merge($return,$data3);
        $return3['data']=$data3;
        $return3['ua_account']=$return['ua_account'];

        if(!$return3){
            return $this->getOUtput(404,'获取失败',$return3);

        }
        return $this->getOUtput(200,'ok',$return3);


    }
    /**
     * 提现申请
     */
    public function requireCash(){
        $unionid=$this->request->param('unionid');
        if(!$unionid){
            return $this->getOutput(400,'unionid 不能为空');
        }
        $model=new UserModel();
        $user=$model->where('unionid',$unionid)->find();
        $money=$this->request->param('money');
        if(empty($money)){
            return $this->getOUtput(401,'请输入提现金额',[]);
        }
        $model2=new UserAccountModel();
        $where=[['ua_user','=',$user['id']]];
        $res2=$model2->where($where)->field('ua_account')->find()->toArray();
        if(100*$money>$res2['ua_account']){
            $res2['ua_account']=$res2['ua_account']/100;
            return $this->getOUtput(400,'余额不足',$res2);
        }
        Db::startTrans();
        try{
            $ua_account=$res2['ua_account']-100*$money;
            $res3=$model2->isUpdate(true)->save(['ua_account'=>$ua_account],['ua_user'=>$user['id']]);
            $addData['cu_user']=$user['id'];
            $addData['cu_time']=time();
            $addData['cu_order']="US".date('YmdHis',$addData['cu_time']).$user['id'];
            $addData['cu_wx_order']="WX".date('YmdHis',$addData['cu_time']).$user['id'];
            $addData['cu_money']=100*$money;
            $addData['cu_type']=2;
            $model4=new CashUsersModel();
            $res4=$model4->data($addData)->save();
            $res5=$model2->where($where)->field('ua_account')->find()->toArray();
            $res5['ua_account']=$res5['ua_account']/100;

            Db::commit();

        }catch (\Exception $e){
            Db::rollback();
            return $this->getOUtput(404,'提现申请失败',$e);
        }

        return $this->getOUtput(200,'提现申请成功，请耐心等待',$res5);


    }




}